package com.pzl.library;

import android.animation.ArgbEvaluator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by zl.peng on 2016-03-02.
 */
public class ColorIndicator extends View implements Indicator {
    private static final String TAG = "ColorIndicator";
    private int mBaseColor = Color.parseColor("#fafafa");
    private int mSelectedColor = Color.parseColor("#ff4545");
    private int mRadius = 20;
    private int mSpace = 10;

    private ViewPager mViewPager;
    private int mPagerAmount;
    private int mCurrentPosition = 0;
    private float mPositionOffset;

    private int mHeight;
    private int mWidth;
    private ArgbEvaluator mArgbEvaluator;
    private int mState;

    private Paint mPaint;

    public ColorIndicator(Context context) {
        this(context, null);
    }

    public ColorIndicator(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ColorIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ColorIndicator, 0, 0);
        mBaseColor = ta.getColor(R.styleable.ColorIndicator_CI_BaseColor, Color.parseColor("#fafafa"));
        mSelectedColor = ta.getColor(R.styleable.ColorIndicator_CI_SelectedColor, Color.parseColor("#ff4545"));
        mRadius = (int) ta.getDimension(R.styleable.ColorIndicator_CI_Radius, 20);
        mSpace = (int) ta.getDimension(R.styleable.ColorIndicator_CI_Space, 10);

        ta.recycle();

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mArgbEvaluator = new ArgbEvaluator();
    }

    @Override
    public void bindViewPager(ViewPager viewPager) {
        if (viewPager == null) throw new IllegalStateException("viewpager is null!");
        if (viewPager.getAdapter() == null)
            throw new IllegalStateException("please set viewpager adapter!");
        mViewPager = viewPager;
        mViewPager.addOnPageChangeListener(this);
        mPagerAmount = mViewPager.getAdapter().getCount();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        mCurrentPosition = position;
        mPositionOffset = positionOffset;
        invalidate();
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        mState = state;
        if (mState == 0) {
            invalidate();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int width, height;
        if (mViewPager == null || widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = getPaddingLeft() + (mRadius * 2 * mPagerAmount) + (mSpace * (mPagerAmount - 1)) + getPaddingRight();
            if (widthMode == MeasureSpec.AT_MOST) {
                width = Math.min(width, widthSize);
            }
        }

        if (mViewPager == null || heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = getPaddingTop() + (mRadius * 2) + getPaddingBottom();
            if (heightMode == MeasureSpec.AT_MOST) {
                height = Math.min(height, heightSize);
            }
        }
        setMeasuredDimension(width, height);

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mHeight = getHeight();
        mWidth = getWidth();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mPagerAmount <= 0) return;
        /**
         * bgcolor
         */
        mPaint.setColor(mBaseColor);
        for (int i = 0; i < mPagerAmount; i++) {
            canvas.drawCircle(mWidth / 2 - (mPagerAmount * (mRadius * 2) + mSpace * (mPagerAmount - 1)) / 2 + mRadius + (2 * mRadius + mSpace) * i, mHeight / 2, mRadius, mPaint);
        }
        /**
         * 渐变色
         */
        mPaint.setColor((int) mArgbEvaluator.evaluate((1 - mPositionOffset), mBaseColor, mSelectedColor));
        canvas.drawCircle(mWidth / 2 - (mPagerAmount * (mRadius * 2) + mSpace * (mPagerAmount - 1)) / 2 + mRadius + (2 * mRadius + mSpace) * mCurrentPosition, mHeight / 2, mRadius, mPaint);
        if (mCurrentPosition + 1 == mPagerAmount) return;
        mPaint.setColor((int) mArgbEvaluator.evaluate(mPositionOffset, mBaseColor, mSelectedColor));
        canvas.drawCircle(mWidth / 2 - (mPagerAmount * (mRadius * 2) + mSpace * (mPagerAmount - 1)) / 2 + mRadius + (2 * mRadius + mSpace) * (mCurrentPosition + 1), mHeight / 2, mRadius, mPaint);

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mViewPager == null) return;
        mViewPager.removeOnPageChangeListener(this);
    }


}
